<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * YB Board Class
 *
 * Board library for Code Igniter.
 *
 * @author		Yashpal Bharadwaj
 * @version		0.5
 * @link		http://
 * @license		MIT License Copyright (c) 2009 Yashpal Bharadwaj
 * @credits		http://
 */

class YB_Board {

    function YB_Board() {
        $this->ci =& get_instance();

        log_message('debug', 'YB Board Libary Initialized');

        // Load required library
        $this->ci->load->library('Session');
        $this->ci->load->database();

        //Load models
        $this->ci->load->model('board/Createmodel');
        $this->ci->load->model('board/Viewmodel');

        // Load YB Board config
        $this->ci->load->config('yb_board');

        // Load language
        //$this->ci->lang->load('dx_auth');

        // Load  event
        //$this->ci->load->library('DX_Auth_Event');

        // Initialize
        $this->_init();
    }

	/* Private function */

    function _init() {
    // Init helper config variable



        $this->YB_can_guest_browse =$this->ci->config->item('YB_can_guest_browse');
        $this->YB_can_view_board   =$this->ci->config->item('YB_can_view_board');
        $this->YB_can_view_forum   =$this->ci->config->item('YB_can_view_forum');
        $this->YB_can_view_category=$this->ci->config->item('YB_can_view_category');
        $this->YB_can_view_thread  =$this->ci->config->item('YB_can_view_thread');
        $this->YB_enable_similies  =$this->ci->config->item('YB_enable_similies');
        $this->YB_view_online      =$this->ci->config->item('YB_view_online');
        $this->YB_view_memberlist  =$this->ci->config->item('YB_view_memberlist');
        $this->YB_view_board_stat  =$this->ci->config->item('YB_view_board_stat');

        //grab value from session
        $this->DX_user_id           =$this->ci->session->userdata('DX_user_id');
        $this->DX_username          =$this->ci->session->userdata('DX_username');
        $this->DX_role_id           =$this->ci->session->userdata('DX_role_id');
        $this->DX_role_name         =$this->ci->session->userdata('DX_role_name');
        $this->DX_parent_roles_id   =$this->ci->session->userdata('DX_parent_roles_id');
        $this->DX_parent_roles_name =$this->ci->session->userdata('DX_parent_roles_name');
        $this->DX_permission        =$this->ci->session->userdata('DX_permission');
        $this->DX_parent_permissions=$this->ci->session->userdata('DX_parent_permissions');
        $this->DX_logged_in         =$this->ci->session->userdata('DX_logged_in');
    }


	/*	// Set session data array
		$user = array(						
			'DX_user_id'						=> $data->id,
			'DX_username'						=> $data->username,
			'DX_role_id'						=> $data->role_id,			
			'DX_role_name'					=> $role_data['role_name'],
			'DX_parent_roles_id'		=> $role_data['parent_roles_id'],	// Array of parent role_id
			'DX_parent_roles_name'	=> $role_data['parent_roles_name'], // Array of parent role_name
			'DX_permission'					=> $role_data['permission'],
			'DX_parent_permissions'	=> $role_data['parent_permissions'],			
			'DX_logged_in'					=> TRUE
		);*/


    function _header_links() {
        $guest_links= '<li>'.anchor(base_url(),'Home').'</li>
				 <li>'.anchor('/auth/login/','Login').'</li>
				 <li>'.anchor('/auth/register','Register').'</li>';


        $member_links= '<li>'.anchor(base_url(),'Home').'</li>
				 <li>'.anchor('/auth/viewprofile/'.$this->ci->session->userdata('DX_user_id'),'View profile').'</li>
				 <li>'.anchor('/user/control_panel/','Control Panel').'</li>
				 <li>'.anchor('/auth/logout/','Logout').'</li>';


        if($this->ci->session->userdata('DX_logged_in'))
            return $member_links;
        else
            return $guest_links;

    }


    function view_board() {
    //If member show data
        if($this->ci->session->userdata('DX_logged_in'))
            return $this->ci->Viewmodel->view_all();

        //If guest can browse
        if(!$this->YB_can_guest_browse)
            return "Please login";

        //for guest just check out permissions
        if($this->YB_can_view_board)
            return $this->ci->Viewmodel->view_all();
        else
            return "Board view is disabled";
    }

    function view_category() {

    //for member show data
        if($this->ci->session->userdata('DX_logged_in'))
            return $this->ci->Viewmodel->view_category($this->ci->uri->segment(3));


        if(!$this->YB_can_guest_browse)
            return "Please login";

        if($this->YB_can_view_category)
            return $this->ci->Viewmodel->view_category($this->ci->uri->segment(3));
        else
            return "Please login";

    }

    function view_forum() {

    //for member show data
        if($this->ci->session->userdata('DX_logged_in'))
            return $this->ci->Viewmodel->view_forum($this->ci->uri->segment(3));


        if(!$this->YB_can_guest_browse)
            return "Please login";

        if($this->YB_can_view_forum)
            return $this->ci->Viewmodel->view_forum($this->ci->uri->segment(3));
        else
            return "Please login";

    }

    function view_thread() {

    //for member show data
        if($this->ci->session->userdata('DX_logged_in'))
            return $this->ci->Viewmodel->view_thread($this->ci->uri->segment(3),$this->ci->uri->segment(4));

        if(!$this->YB_can_guest_browse)
            return "Please login";

        if($this->YB_can_view_thread)
            return $this->ci->Viewmodel->view_thread($this->ci->uri->segment(3),$this->ci->uri->segment(4));
        else
            return "Please login";

    }

    function board_stat() {
        return $this->ci->Viewmodel->board_stat();
    }

    function _thread_edit_links() {
        $links= '<li>'.anchor(base_url(),'Quote').'</li>
                         <li>'.anchor('/auth/login/','Edit').'</li>
                         <li>'.anchor('/auth/login/','Delete').'</li>';


        if($this->ci->session->userdata('DX_logged_in'))
            return $member_links;
        else
            return "";

    }

    function _small_top_links() {
        $links= '<li>'.anchor(base_url(),'Home').'</li>';

        if($this->ci->uri->segment(3)==TRUE && $this->ci->uri->segment(4)==FALSE) {

            $links.='<li> &raquo; </li>
                 <li>'.anchor($this->ci->uri->segment(1).'/'.$this->ci->uri->segment(2).'/'.$this->ci->uri->segment(3),$this->ci->uri->segment(3)).'</li>';


        }

        if($this->ci->uri->segment(4)==TRUE) {

            $links.='<li> &raquo; </li>
                          <li>'.anchor('board/view_forum/'.$this->ci->uri->segment(3),$this->ci->uri->segment(3)).'</li>
                          <li> &raquo; </li>
                          <li>'.anchor($this->ci->uri->segment(1).'/'.$this->ci->uri->segment(2).'/'.$this->ci->uri->segment(3).'/'.$this->ci->uri->segment(4),$this->ci->uri->segment(4)).'</li>';

        }

        return $links;
    }





  /*home - >   category  ->  forum  -> create thread
     home - >   category  ->  forum  -> create reply
     home - >   category  ->  forum  -> edit thread
     home - >   category  ->  forum  -> edit reply
     home - >   category  ->  forum  -> thread title
     fix        uri -> 3      uri->4    uri-> 5


    //uri 1 and 2 are controller and functions*/


    /**
     * If user is logged in then show create a new thread {button} in forum section only
     * If user in thread view then one more {buton} i.e post reply
     * In category and index no such buttons are available
     *
     */
    function _create_buttons() {

    //if guest send nothing
        if(!$this->ci->session->userdata('DX_logged_in'))
            return "";

        //now check where we are
        //view_category  view_forum view_thread
        $c2=$this->ci->uri->segment(2);
        $c3=$this->ci->uri->segment(3);
        $c4=$this->ci->uri->segment(4);

        switch($c2) {

            case 'view_category':  return ""; break;
            case 'view_forum':
                return '<li>'.anchor(site_url().'/action/create_thread/'.$c3,'Create Thread').'</li>';
                break;
            case 'view_thread':
                $links= '<li>'.anchor(site_url().'/action/create_thread/'.$c3,'Create Thread').'</li>
                            <li>'.anchor(site_url().'/action/create_reply/'.$c3.'/'.$c4,'Post Reply').'</li>';
                  return  $links;
                break;
        }
    }


    /**
     * Normal buttons for all users
     * quote
     * multiquote -- next minor release
     *
     * Moderator buttons
     * delete
     * edit
     *
     * Ignore member - make ignore table
     * Report Topic -
     *
     */

     function _normal_buttons()
     {
         
     }

     function _moderator_buttons()
     {

     }

     function _extra_buttons()
     {

     }
}
?>